const Koa = require("koa");

const KoaRouter = require("koa-router");
const { getSignInfo, init } = require("./task");
const KoaBodyParser = require("koa-bodyparser");
const PORT = 5004;
const app = new Koa();

const logger = require("./utils/log");

app.use(KoaBodyParser());

const router = new KoaRouter();

router.post("/sign", async (ctx, next) => {
  logger.info(
    "controller",
    ctx.request.body.url,
    ctx.request.body.proxy_url,
    ctx.request.body.work_order_id,
    ctx.request.body.submit_type
  );
  if (ctx.request.body?.url) {
    ctx.body = await getSignInfo(
      ctx.request.body.url,
      ctx.request.body.proxy_url,
      ctx.request.body.work_order_id,
      ctx.request.body.submit_type
    );
  }
  await next();
});

app.use(router.routes()).use(router.allowedMethods());

app.listen(PORT, async () => {
  await init();
  console.log("server is running at 127.0.0.1:5004");
});
